https://leetcode.cn/problems/move-zeroes/description/
//左右指针
class Solution {
public:
    //非0  || 0 || 未处理
    void moveZeroes(vector<int>& nums) {
        int left = 0;
        int right = 0;

        while (left <= right && right < nums.size())
        {
            if (nums[right] == 0 || right == left)
            {
                //std::cout <<nums[right];
                ++right;
            }
            else
            {
                while (left < right && right < nums.size())
                {
                    if (nums[left] == 0)
                    {
                        int tmp = nums[left];
                        nums[left] = nums[right];
                        nums[right] = tmp;
                        break;
                    }
                    ++left;
                }
            }
        }
    }
};
//优化后
class Solution {
public:
    void moveZeroes(vector<int>& nums) {

        for(int left = 0,right = 0;right < nums.size();++right)
        {
            if(nums[right] != 0)
            {
                swap(nums[right],nums[left]);
                ++left;
            }
        }
    }
};
//前后指针  注：该方法会改变相对位置
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int head = 0;
        int tail = nums.size() - 1;
        while (head < tail)
        {
            if (nums[tail] == 0)--tail;
            else
            {
                while (head < tail)
                {
                    if (nums[head] == 0)
                    {
                        nums[head] = nums[tail];
                        nums[tail] = 0;
                        break;
                    }

                    ++head;
                }
            }
        }
    }
};
